<template>
  <div>
    <el-row  type="flex" class="row-bg">
      <el-col>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="搜索">
          <el-input v-model="formInline.searchVal" placeholder="请输入经纪人姓名"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.selectedVal" clearable  @change="selectChange" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="FindSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      </el-col>
      <el-col><el-button type="primary" @click="addBtn">新建</el-button></el-col>
    </el-row>

    <el-table border  :data="tableData" stripe style="width: 100%">
      <el-table-column align="center" type="index" label="序号"></el-table-column>
      <el-table-column  align="center" prop="name" label="姓名"> </el-table-column>
      <el-table-column  align="center" prop="phoneNum" label="手机号"> </el-table-column>
      <el-table-column  align="center" prop="company" label="公司"> </el-table-column>
      <el-table-column  align="center" prop="newHouse" label="新房"> </el-table-column>
      <el-table-column  align="center" prop="secondHouse" label="二手房"> </el-table-column>
      <el-table-column  align="center" prop="tenement" label="租房"> </el-table-column>
      <el-table-column  align="center" prop="status" label="状态">
        <el-tag slot-scope="scoped" :type="scoped.row.status=='在职'?'success':'warning'">{{scoped.row.status}}</el-tag>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scoped">
          <el-button @click="editBtn(scoped.row)" size="mini" type="warning">编辑</el-button>
          <el-button @click="delData(scoped.row)" size="mini" type="danger">删除</el-button>
          <el-button @click="delData(scoped.row)" size="mini" type="success">启用</el-button>
          <el-button @click="delData(scoped.row)" size="mini" type="info">禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageindex"
          :page-sizes="[5, 10, 15]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog
      v-if="dialogVisible"
      :title="dialogType"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="diaLogClose">
      <el-form :model="brokerForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item  label="姓名" prop="name">
          <el-input  v-model="brokerForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNum">
          <el-input  v-model="brokerForm.phoneNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input  v-model="brokerForm.company" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新房" prop="newHouse">
          <el-input  v-model="brokerForm.newHouse" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="二手房" prop="secondHouse">
          <el-input  v-model="brokerForm.secondHouse" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租房" prop="tenement">
          <el-input  v-model="brokerForm.tenement" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="brokerForm.status" clearable  placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api'
export default {
  data () {
    return {
      options: [{
        value: '在职',
        label: '在职'
      }, {
        value: '离职',
        label: '离职'
      }],
      formInline: {
        searchVal: '',
        selectedVal: ''
      },
      dialogType: '',
      dialogVisible: false,
      brokerForm: {
        id: '',
        name: '',
        phoneNum: '',
        company: '',
        newHouse: '',
        secondHouse: '',
        tenement: '',
        status: ''
      },
      tableData: [],
      total: 0,
      pageindex: 1,
      pagesize: 10
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (!newVal) {
        for (const key in this.brokerForm) {
          if (typeof this.brokerForm[key] === 'boolean') {
            this.brokerForm[key] = false
          } else {
            this.brokerForm[key] = ''
          }
        }
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { pageindex, pagesize, formInline } = this
      const { searchVal: keyWord, selectedVal: status } = formInline
      const { data } = await api.getbrokerList({ pageindex, pagesize, keyWord, status })
      if (data.meta.status) {
        this.tableData = data.data.list
        this.total = data.data.total
      }
      console.log(data)
    },
    // 筛选在职离职
    selectChange () {
      this.getList()
    },
    editBtn (row) {
      this.dialogVisible = true
      this.dialogType = '编辑'
      for (const key in row) {
        this.brokerForm[key] = row[key]
      }
    },
    addBtn () {
      this.dialogVisible = true
      this.dialogType = '添加'
    },
    async editData () {
      const { brokerForm } = this
      const { data } = await api.editBrokerList({ brokerForm })
      if (data.meta.status) {
        this.dialogVisible = false
        this.getList()
        return this.$message.success(data.meta.msg)
      }
      return this.$message.error(data.meta.msg)
    },
    async addData () {
      const { brokerForm } = this
      const { data } = await api.addBrokerList({ brokerForm })
      if (data.meta.status) {
        this.dialogVisible = false
        this.getList()
        return this.$message.success(data.meta.msg)
      }
      return this.$message.error(data.meta.msg)
    },
    async delData (row) {
      const { id, name } = row
      try {
        await this.$confirm('确认删除' + name + '吗？')
        const { data } = await api.deleteBrokerList({ id })
        if (data.meta.status) {
          this.$message.success(data.meta.msg)
          this.getList()
        } else {
          this.$message.error(data.meta.msg)
        }
      } catch (error) {
        this.$message.info('取消删除')
      }
    },
    formSubmit () {
      switch (this.dialogType) {
        case '添加':
          this.addData()
          break
        case '编辑':
          this.editData()
          break
      }
    },
    FindSubmit () {
      this.getList()
    },
    diaLogClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible = false
        })
        .catch(_ => {})
    },
    handleSizeChange (size) {
      this.pagesize = size
      this.getList()
    },
    handleCurrentChange (index) {
      this.pageindex = index
      this.getList()
    }
  }

}
</script>

<style lang="less" scoped>

</style>
